package smashedapples.containers
{
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.ui.Keyboard;

	import mx.containers.Canvas;
	import mx.events.FlexEvent;
	import mx.managers.PopUpManager;

	public class Popup extends Canvas
	{
		public function Popup()
		{
			super();
			this.addEventListener( FlexEvent.CREATION_COMPLETE, handleCreationComplete );

		}

		[Inspectable( enumeration="true,false",defaultValue="false" )]
		public var closeOnEscape:Boolean = true;

		protected function handleCreationComplete( event:FlexEvent ):void
		{
			this.systemManager.addEventListener( KeyboardEvent.KEY_DOWN, handleKeyDown, true );
		}

		protected function handleKeyDown( event:KeyboardEvent ):void
		{
			if( event.charCode == Keyboard.ESCAPE && this.closeOnEscape == true )
				this.closePopup();
		}

		protected function closePopup( event:Event = null ):void
		{
			PopUpManager.removePopUp( this );
		}

	}
}